package com.jjty.dataGenerator.bg.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jjty.dataGenerator.bg.entity.ProvinceData;
import com.jjty.dataGenerator.bg.entity.ProvinceInfo;
import com.jjty.dataGenerator.bg.entity.SchoolInfo;
import com.jjty.dataGenerator.bg.service.ProvinceService;
import com.jjty.dataGenerator.common.ApiResult;
import com.jjty.dataGenerator.common.PageParamReader;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * <p>
 * 省份信息 前端控制器
 * </p>
 *
 * @author yourname
 * @since 2024-xx-xx
 */
@RestController
@RequestMapping("/data/province")
public class ProvinceController {

    @Autowired
    private ProvinceService provinceService;

    /**
     * 获取所有省份信息
     * @return 省份列表
     */
    @RequestMapping("/list")
    public ApiResult getProvinces() {
        List<ProvinceInfo> provinces = provinceService.getAllProvinces();
        return ApiResult.querySuccess(provinces);
    }

    /**
     * 新增对接省份
     * @param provinceName 省份信息
     * @param dataType 数据类型
     * @return 操作结果
     */
    @RequestMapping("/add")
    public ApiResult addProvince(String provinceName, String dataType) {
        boolean result = provinceService.saveProvince(provinceName, dataType);
        if (result) {
            ProvinceData provinceData = new ProvinceData();
            provinceData.setProvinceName(provinceName);
            provinceData.setDataType(dataType);
            return ApiResult.doSuccess("新增省份成功", provinceData);
        } else {
            return ApiResult.doFailure("新增省份失败");
        }
    }
    /**
     * 查询对接省份
     * @return 操作结果
     */
    @RequestMapping("/queryByPage")
    public ApiResult listProvinces(HttpServletRequest request, ProvinceData param, String keyword) {
        Page page = PageParamReader.read(request);
        page = provinceService.getProvinceInfoPage(page,param,keyword);
        return ApiResult.querySuccess(page);
    }
    /**
     * 查询省份对应的数据类型
     */
    @RequestMapping("/getProvinceDataType")
    public ApiResult getProvinceDataType(String provinceName) {
        List<ProvinceData> dataTypes = provinceService.getProvinceDataType(provinceName);
        if (dataTypes != null && !dataTypes.isEmpty()) {
            return ApiResult.querySuccess(dataTypes);
        } else {
            return ApiResult.doFailure("未找到该省份对应的数据类型");
        }
    }
    /**
     * 查询需要对接的省份
     */
    @RequestMapping("/getProvince")
    public ApiResult getProvince() {
        List<ProvinceData> provinces = provinceService.getProvince();
        return ApiResult.querySuccess(provinces);
    }
}